package com.example.xianji.utils.exit_app;

import android.app.Activity;
import android.view.View;
import com.example.xianji.activity.MainActivity;
import com.example.xianji.R;
import com.example.xianji.widgets.CircleAlertDialog;

import java.util.Stack;

/**
 * 作者：张玉辉 on 2018/4/23 10:11.
 */
public class XJActivityStack extends WeakArrayList<Activity> {
    private static final String TAG = "ActivityStack";

    private static Stack<Activity> activityStack;
    private static XJActivityStack instance;

    private XJActivityStack() {
    }

    private static class Holder{
        private static final XJActivityStack instance = new XJActivityStack();
    }

    public static XJActivityStack getInstance(){
        return Holder.instance;
    }

    /** 添加Activity到堆栈 */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    public void removeActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        if (activity != null) {
            activityStack.remove(activity);
            activity = null;
        }
    }

    public MainActivity getMainActivity() {
        if (activityStack == null)
            return null;
        for (Activity activity : activityStack) {
            if (activity instanceof MainActivity) {
                return (MainActivity) activity;
            }
        }
        return null;
    }

    /** 获取当前Activity（堆栈中最后一个压入的） */
    public Activity currentActivity() {
        if (activityStack == null)
            return null;

        Activity activity = activityStack.lastElement();

        while (activity.isFinishing()) {
            activityStack.remove(activity);
            if (activityStack.size() <= 0) {
                break;
            }
            activity = activityStack.lastElement();
        }
        return activity;
    }

    /** 结束当前Activity（堆栈中最后一个压入的） */
    public void finishActivity() {
        if (activityStack == null)
            return;

        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /** 结束指定的Activity */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            if (!activity.isFinishing()) {
                activity.finish();
            }
            activity = null;
        }
    }

    /** 结束指定类名的Activity */
    public void finishActivity(Class<?> cls) {
        if (activityStack == null)
            return;

        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /** 结束所有Activity */
    public void finishAllActivity() {
        if (activityStack == null)
            return;

        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    public int getActivityCount() {
        if (activityStack == null)
            return 0;

        return activityStack.size();
    }

    /** 退出应用程序 */
    public void AppExit(boolean showConfirm) {
        if (showConfirm) {
            showExitConfirm();
        } else {
            killProcess();
        }
    }

    private void showExitConfirm() {
        Activity act = currentActivity();
        if (act == null) {
            killProcess();
            return;
        }

        new CircleAlertDialog(act)
                .builder()
                .setTitle(
                        act.getResources()
                                .getString(R.string.is_exit_app))
                .setCancelable(false)
                .setPositiveButton(
                        act.getResources().getString(R.string.sure),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                killProcess();
                            }
                        })
                .setNegativeButton(
                        act.getResources().getString(R.string.cancel),
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                            }
                        }).show();
    }

    private void killProcess() {
        try {
            finishAllActivity();
//            ActivityManager activityMgr = (ActivityManager) BBSApplication
//                    .getInstance().getApplicationContext()
//                    .getSystemService(Context.ACTIVITY_SERVICE);
//            activityMgr.killBackgroundProcesses(BBSApplication
//                    .getInstance().getApplicationContext().getPackageName());
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }
}
